package com.webserver.core;

import com.webserver.core.ClientHandler;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.File;
import java.net.URISyntaxException;

public class DispatcherServlet {
    private static File staticDir,rootDir;

    static {
        try {
            rootDir = new File(ClientHandler.class.getClassLoader()
                    .getResource(".").toURI());
            staticDir = new File(rootDir,"static");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

    }


    public void servlet(HttpServletRequest request, HttpServletResponse response){
        String path = request.getUri();//需要获得request的uri
        File file = new File(staticDir,path);
        System.out.println("该资源文件是否存在：" + file.exists());
        if (!file.isFile()) {
            file = new File(staticDir, "/root/404.html");
            response.setStatusCode(404);//需要设置response的状态码
            response.setStatusReason("NotFound");//需要设置response的状态描述
        }
        response.setContentFile(file);
    }



}
